﻿using InfoService.Enums;
using InfoService.Settings.Data;
using MediaPortal.GUI.Library;
using InfoService.Settings;

namespace InfoService.Utils
{
    public static class RecentlyAddedUtils
    {
        public static void SaveLastRecentlyAddedItems()
        {
            SettingsManager.Properties.RecentlyAddedSettings.RecentlyAdded.Clear();
            for (int i = 1; i <= 3; i++)
            {
                SettingsRecentlyAddedItem srai = new SettingsRecentlyAddedItem();
                srai.Title = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Title, "series", i));
                srai.Season = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Season, "series", i));
                srai.EpisodeNumber = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Episodenumber, "series", i));
                srai.EpisodeTitle = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Episodetitle, "series", i));
                if (!string.IsNullOrEmpty(srai.Title) && !string.IsNullOrEmpty(srai.Season) && !string.IsNullOrEmpty(srai.EpisodeNumber) && !string.IsNullOrEmpty(srai.EpisodeTitle))
                {
                    srai.Title = srai.Title.Trim();
                    srai.Season = srai.Season.Trim();
                    srai.EpisodeNumber = srai.EpisodeNumber.Trim();
                    srai.EpisodeTitle = srai.EpisodeTitle.Trim();
                    if (!string.IsNullOrEmpty(srai.Title) && !string.IsNullOrEmpty(srai.Season) && !string.IsNullOrEmpty(srai.EpisodeNumber) && !string.IsNullOrEmpty(srai.EpisodeTitle))
                    {
                        srai.Thumb = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Thumb, "series", i));
                        srai.Fanart = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Fanart, "series", i));
                        srai.Type = RecentlyAddedType.Series;
                        SettingsManager.Properties.RecentlyAddedSettings.RecentlyAdded.Add(srai);
                    }
                }
            }
            for (int i = 1; i <= 3; i++)
            {
                SettingsRecentlyAddedItem srai = new SettingsRecentlyAddedItem();
                srai.Title = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Title, "movie", i));
                if (!string.IsNullOrEmpty(srai.Title))
                {
                    srai.Title = srai.Title.Trim();
                    if (!string.IsNullOrEmpty(srai.Title)) 
                    {
                        srai.Thumb = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Thumb, "movie", i));
                        srai.Fanart = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Fanart, "movie", i));
                        srai.Type = RecentlyAddedType.Movie;
                        SettingsManager.Properties.RecentlyAddedSettings.RecentlyAdded.Add(srai);
                    }
                }
            }
        }

        public static void LoadLastRecentlyAddedItems()
        {
            int i = 1;
            foreach (SettingsRecentlyAddedItem item in SettingsManager.Properties.RecentlyAddedSettings.RecentlyAdded)
            {
                switch (item.Type)
                {
                    case RecentlyAddedType.Series:
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Title, "series", i), item.Title);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Season, "series", i), item.Season);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Episodenumber, "series", i), item.EpisodeNumber);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Episodetitle, "series", i), item.EpisodeTitle);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Thumb, "series", i), item.Thumb);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Fanart, "series", i), item.Fanart);
                        break;
                    case RecentlyAddedType.Movie:
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Title, "movie", i), item.Title);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Thumb, "movie", i), item.Thumb);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Fanart, "movie", i), item.Fanart);
                        break;
                }
                i++;
            }
        }
    }
}
